package com.dhcc.bpm.alarm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.alarm.config.RedisKey;
import com.dhcc.bpm.alarm.entity.AlarmObject;
import com.dhcc.bpm.alarm.service.IAlarmObjectService;
import com.dhcc.bpm.alarm.vo.AlarmObjectVO;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 *  控制器
 *
 * @author xiafayu
 * @since 2022-02-16
 */
@RestController
@AllArgsConstructor
@NoArgsConstructor
@RequestMapping("/alarm/alarmObject")
@Api(value = "告警对象表", tags = "接口")
public class AlarmObjectController extends BladeController {
	private static final Logger log = LoggerFactory.getLogger(AlarmObjectController.class);

	@Autowired
	private IAlarmObjectService alarmObjectService;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private ISerialNumberService serialNumberService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入alarmObject")
	public R<AlarmObject> detail(AlarmObject alarmObject) {
		AlarmObject detail = alarmObjectService.getOne(Condition.getQueryWrapper(alarmObject));
		return R.data(detail);
	}

	/**
	 * 分页 
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入alarmObject")
	public R<IPage<AlarmObject>> list(AlarmObject alarmObject, Query query) {
		IPage<AlarmObject> pages = alarmObjectService.page(Condition.getPage(query), Condition.getQueryWrapper(alarmObject));
		return R.data(pages);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping("/qryList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入alarmObject")
	public R<IPage<AlarmObjectVO>> page(AlarmObjectVO alarmObject, Query query) {
		IPage<AlarmObjectVO> pages = alarmObjectService.selectAlarmObjectPage(Condition.getPage(query), alarmObject);
		return R.data(pages);
	}

	/**
	 * 新增 
	 */
	@PostMapping("/add")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入alarmObject")
	public R save(@Valid @RequestBody AlarmObject alarmObject) {
		return R.status(alarmObjectService.save(alarmObject));
	}

	/**
	 * 修改 
	 */
	@PostMapping("/edit")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入alarmObject")
	public R update(@Valid @RequestBody AlarmObject alarmObject) {
		boolean flag = alarmObjectService.updateById(alarmObject);
		if(flag){
			if(!"1".equals(alarmObject.getIenable())){
				redisUtil.del(RedisKey.ALARM_OBJECT_KEY+alarmObject.getId());
			}else {
				redisUtil.set(RedisKey.ALARM_OBJECT_KEY+alarmObject.getId(),alarmObject,60*60);
			}
		}
		return R.status(flag);
	}

	/**
	 * 新增或修改 
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入alarmObject")
	public R submit(@Valid @RequestBody AlarmObject alarmObject) {
		//开发告警对象的redis缓存,使用String 数据类型存储
		boolean  flag = alarmObjectService.saveOrUpdate(alarmObject);
		if(flag){
			if(!"1".equals(alarmObject.getIenable())){
				redisUtil.del(RedisKey.ALARM_OBJECT_KEY+alarmObject.getId());
			}else {
				redisUtil.set(RedisKey.ALARM_OBJECT_KEY+alarmObject.getId(),alarmObject,60*60);
			}
		}
		return R.status(flag);
	}
	
	/**
	 * 新增或修改 
	 */
//	@PostMapping("/submit")
//	@ApiOperationSupport(order = 6)
//	@ApiOperation(value = "新增或修改", notes = "传入alarmObject 传入alarmObject, alarmObjectGroupList")
//	public R submit(@Valid @RequestBody JSONObject request) {
//		//return R.status(alarmObjectService.saveOrUpdate(alarmObject));
//		
//		AlarmObject alarmObject = request.getObject("alarmObject", AlarmObject.class);
//    	
//		String alarmObjIds = request.getString("alarmGroupIds");
//		List<String> alarmGroupIdList = Arrays.asList(alarmObjIds.split(","));
//        
//        return R.status(alarmObjectService.saveOrUpdate(alarmObject, alarmGroupIdList));
//	}

	
	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<Long> idList = Func.toLongList(ids);
		boolean flag = alarmObjectService.removeByIds(idList);
		if(flag){
			for (Long id: idList) {
				redisUtil.del(RedisKey.ALARM_OBJECT_KEY+id);
			}
		}
		return R.status(flag);
	}


	/**
	 * @return org.springblade.core.tool.api.R
	 * @Description 自动生成对象编码
	 * @Author xfy
	 * @Date 15:30 2022/03/09
	 **/
	@GetMapping("/autoCreateCode")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "自动生成对象编码", notes = "传入app")
	@ApiLog("自动生成对象编码")
	@Log(title = "自动生成对象编码", businessType = BusinessType.OTHER)
	public R autoCreateCode() {
		try {
			String objNo = serialNumberService.generateSerialNumberByModelCode("amarm_object");
			return R.data(objNo);
		} catch (Exception e) {
			log.error("自动生成服务编码失败:{}", e);
			return R.status(false);
		}
	}
	
}
